<?php
/**
 * Created by PhpStorm.
 * User: Sixstar-Peter
 * Date: 2019/4/13
 * Time: 20:48
 */
include "waitGroup.php";

$server = new  Swoole\Http\Server('0.0.0.0', 9800);
$server->on('request', function ($req, $response) {

    if ($req->server['request_uri'] == '/favicon.ico') {
        //$response->end('111');
        return;
    }
//    $time=time();
//    $swoole_mysql = new Swoole\Coroutine\MySQL();
//    $swoole_mysql->connect([
//        'host' => '127.0.0.1',
//        'port' => 3306,
//        'user' => 'root',
//        'password' => 'Peter123!',
//        'database' => 'test',
//    ]);
//    $swoole_mysql->setDefer(); //延迟收包如果不设置会阻塞执行
//    $res =$swoole_mysql->query('select sleep(3)'); //发送请求
//    //var_dump($res);
//
//    $swoole_mysql1 = new Swoole\Coroutine\MySQL();
//    $swoole_mysql1->connect([
//        'host' => '127.0.0.1',
//        'port' => 3306,
//        'user' => 'root',
//        'password' => 'Peter123!',
//        'database' => 'test',
//    ]);
//    $swoole_mysql1->setDefer(); //延迟收包
//    $res = $swoole_mysql1->query('select sleep(3)');
//
//    //延迟收包,然后统一获取
//    var_dump($swoole_mysql->recv(),$swoole_mysql1->recv());
//    echo time()-$time.PHP_EOL;
//    $response->end("time:".(time()-$time));

    //$chan = new chan(2); //通道的容量
    //for(){} 可能要根据客户端要求我发送n条请求
//    go(function () use ($chan) {
//        $cli = new Swoole\Coroutine\Http\Client('www.qq.com', 80);
//        $cli->set(['timeout' => 10]);
//        $cli->setHeaders([
//            'Host' => "www.qq.com",
//            "User-Agent" => 'Chrome/49.0.2587.3',
//            'Accept' => 'text/html,application/xhtml+xml,application/xml',
//            'Accept-Encoding' => 'gzip',
//        ]);
//        $ret = $cli->get('/');
//        $chan->push(['www.qq.com' => $cli->statusCode]);
//    });
//
//    go(function () use ($chan) {
//        $cli = new Swoole\Coroutine\Http\Client('www.163.com', 80);
//        $cli->set(['timeout' => 10]);
//        $cli->setHeaders([
//            'Host' => "www.163.com",
//            "User-Agent" => 'Chrome/49.0.2587.3',
//            'Accept' => 'text/html,application/xhtml+xml,application/xml',
//            'Accept-Encoding' => 'gzip',
//        ]);
//        $ret = $cli->get('/');
//        $chan->push(['www.163.com' => $cli->statusCode]);
//    });
//
//    //统一收包,延迟收包
//    $result = [];
//    for ($i = 0; $i < 2; $i++)
//    {
//        $result += $chan->pop();
//    }
//    var_dump($result);

    $wait = new waitGroup();
    $wait->add(); //每启动一个协程+1
    go(function () use ($wait) {
        $cli = new Swoole\Coroutine\Http\Client('www.qq.com', 80);
        $cli->set(['timeout' => 10]);
        $cli->setHeaders([
            'Host' => "www.qq.com",
            "User-Agent" => 'Chrome/49.0.2587.3',
            'Accept' => 'text/html,application/xhtml+xml,application/xml',
            'Accept-Encoding' => 'gzip',
        ]);
        $ret = $cli->get('/');
        $wait->push(['www.qq.com' => $cli->statusCode]);
    });
    $wait->add();
    go(function () use ($wait) {
        $cli = new Swoole\Coroutine\Http\Client('www.163.com', 80);
        $cli->set(['timeout' => 10]);
        $cli->setHeaders([
            'Host' => "www.163.com",
            "User-Agent" => 'Chrome/49.0.2587.3',
            'Accept' => 'text/html,application/xhtml+xml,application/xml',
            'Accept-Encoding' => 'gzip',
        ]);
        $ret = $cli->get('/');
        $wait->push(['www.163.com' => $cli->statusCode]);
    });
    $result = $wait->wait(); //阻塞接收协程返回的结果
    $response->end(json_encode($result));
});

$server->start();